﻿using JaxaraRnD.Helpers.DataAccess;
using JaxaraRnD.Models.Objects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace JaxaraRnD.Controllers
{
    public class ProductController : Controller
    {
        private readonly IRepository<Product> _productRepository;
        private readonly IRepository<Order> _orderRepository;

        public ProductController()
        {
            _productRepository = new Repository<Product>();
            _orderRepository = new Repository<Order>();
        }

        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View(_productRepository.GetAll());
        }

        //
        // GET: /Product/Details/5
        public ActionResult Details(int id)
        {
            Product product = _productRepository.Get(id);

            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            Product product = new Product();
            ViewBag.OrderSelectList = GetOrderSelectList(product);
            return View();
        }

        private dynamic GetOrderSelectList(Product product)
        {
            var allOrders = _orderRepository.GetAll();
            var orders = allOrders.Select(p => new SelectListItem()
            {
                Text = p.Id.ToString(),
                Value = p.Id.ToString(),
                Selected = product.Orders.FirstOrDefault(o => p.Id == o.Id) != null
            }).OrderBy(p => p.Text).ToArray();

            return orders;
        }


        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            Product product = new Product();

            if (TryUpdateModel(product) && ModelState.IsValid)
            {
                product.Orders = new List<Order>();
                var sOrders = collection["Orders.List"];

                if (sOrders != null)
                {
                    int[] selectedOrders = sOrders.Split(new[] { ',' }).Select(n => Convert.ToInt32(n)).ToArray();
                    product.Orders = _productRepository.GetOrdersByOrderids(selectedOrders);
                }

                _productRepository.SaveOrUpdate(product);

                return RedirectToAction("Index");
            }

            ViewBag.OrderSelectList = GetOrderSelectList(product);
            return View();
        }
        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id)
        {
            Product product = _productRepository.Get(id);
            ViewBag.OrderSelectList = GetOrderSelectList(product);
            return View(product);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Product product = _productRepository.Get(id);

            if (TryUpdateModel(product) && ModelState.IsValid)
            {
                product.Orders = new List<Order>();

                var selectedOrders = collection["Orders.List"].Split(new[] { ',' }).Select(n => Convert.ToInt32(n)).ToArray();
                product.Orders = _productRepository.GetOrdersByOrderids(selectedOrders);

                _productRepository.SaveOrUpdate(product);
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id)
        {
            _productRepository.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
